﻿using AutoMapper;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.ProcessCategory;
using ReturnValue;
using Yitter.IdGenerator;

namespace MES.Server.API.Application.Handler.ProcessCategory
{
    public class AddProcessCategoryHandler : IRequestHandler<AddProcessCategoryCommand, ApiResult<int>>
    {
        private readonly IRepository<ProcessCategoryModel> categoryRepo;
        private readonly IMapper mapper;

        public AddProcessCategoryHandler(IRepository<ProcessCategoryModel> CategoryRepo,IMapper mapper)
        {
            categoryRepo = CategoryRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(AddProcessCategoryCommand request, CancellationToken cancellationToken)
        {
            var category = mapper.Map<ProcessCategoryModel>(request);
            category.Id = YitIdHelper.NextId();
            var r= await categoryRepo.AddAsync(category);
            if (r > 0)
            {
                return new ApiResult<int>().AddSuccess("工艺分类", r);
            }else
            {
                return new ApiResult<int>().AddFail("工艺分类");
            }
        }
    }
}
